const { resolve } = require('path')
const express = require('express'),
  app = express()

app.listen(2233, _ => {
  console.log('ok')
})
// 跨域
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', "http://127.0.0.1:5500")
  res.header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, Accept, X-Requested-With")
  res.header("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, HEAD, OPTIONS")
  if (req.method == 'OPTION') { 
    res.send('ok')
    return 
  }
  next()
})
app.use('/uploads', express.static( __dirname + '/uploads'))
const multer = require('multer')
const upload = multer({ dest: __dirname + '/uploads' })
app.use('/upload', upload.single('file'), async (req, res) => {
  const file = req.file
  console.log(file)
  // console.log(req.param)
  // console.log(req)
  file.url = `http://127.0.0.1:2233/uploads/${file.name}`
  res.send(file)
})

const path = require('path')
const express = require('express'),
  app = express()

app.listen(2233, _ => {
  console.log('ok')
})
// 跨域
app.use((req, res, next) => {
  res.header('Access-Control-Allow-Origin', "http://127.0.0.1:5500")
  res.header("Access-Control-Allow-Headers", "Content-Type, Content-Length, Authorization, Accept, X-Requested-With")
  res.header("Access-Control-Allow-Methods", "PUT, POST, GET, DELETE, HEAD, OPTIONS")
  if (req.method == 'OPTION') { 
    res.send('ok')
    return 
  }
  next()
})
app.use('/uploads', express.static( __dirname + '/uploads'))
const multer = require('multer')
const storage = multer.diskStorage({
  destination: (req, file, cb) => {
    cb(null, path.join(__dirname, '/uploads'))
  },
  filename: (req, file, cb) => {
    cb(null, file.fieldname)
  }
})
const upload = multer({ storage })
app.use('/upload', upload.any('file'), async (req, res) => {
  const file = req.files
  console.log(file)
  // file.url = `http://127.0.0.1:2233/uploads/${file.name}`
  res.send(file)
})